<?php
namespace Home\Controller;
use Think\Controller;

class OrderController extends Controller {
    private $user;

    public function _initialize(){
    	$this->user = session('user');
    	// var_dump($this->user);
    	if($this->user == null){
    		redirect(U('/Home/User/login'), 2, '尚未登录，请登录...');
    		exit;
    	}
    }


    /**
     * addOrder
     * 添加订单
     * @access public
     *
     * @return mixed Value.
     */
    public function addOrder() {
    	$printType = I('printType');
    	$data['printtype'] = $printType;
    	$data['user_id'] = $this->user['user_id'];
    	$data['number'] = I('number');
    	$data['remark'] = I('remark');
    	$data['updatetime'] = time();
    	if(!is_numeric((int)$data['number']) || (int)$data['number'] <= 0){
    		$result = array('code' => 201, 'msg' => '数量错误', 'data' => I('number'));

    		echo json_encode($result);
    		exit;
    	}

    	switch ($printType) {
    		case '1':
    			$data['textstandard1'] = I('textStandard1');
    			$data['textstandard2'] = I('textStandard2');
    			$data['textstandard3'] = I('textStandard3');
    			break;
    		case '2':
    			$data['photostandard1'] = I('photoStandard1');
    			$data['photostandard2'] = I('photoStandard2');
    			break;
    		case '3':
    			$data['bookstandard1'] = I('bookStandard1');
    			$data['bookstandard2'] = I('bookStandard2');
    			$data['bookstandard3'] = I('bookStandard3');
    			break;
    		case '4':
    			$data['portablestandard1'] = I('portableStandard1');
    			break;
    		case '5':
    			break;
    		case '6':
    			$data['dmstandard1'] = I('DMStandard1');
    			$data['dmstandard2'] = I('DMStandard2');
    			$data['dmstandard3'] = I('DMStandard3');
    			break;
    		case '7':
    			break;
    		case '8':
    			break;
    		case '9':
    			break;
    		default:
    			break;
    	}

    	$data['status'] = 0;

    	// 储存文件
        $upload = new \Think\Upload();//实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts  = array('doc', 'docx', 'xls', 'xlsx', 'jpg', 'png', 'gif', 'ppt', 'pptx', 'wps');//设置上传类型限制
         $upload->rootPath = './Public/Upload/'; // 设置附件上传根目录
    	$upload->savePath = 'files'; // 设置附件上传（子）目录
        $upload->saveName = $this->user['user_id'].'_'.time();//保存文件的命名规则，这里以时间戳为文件名
        $result = $upload->uploadOne($_FILES['file']);
        if( !$result ) {
			$result = array('code' => 203, 'msg' => $upload->getError(), 'data' => $_FILES['file']);

			echo json_encode($result);
			exit;
        } else {
            $data['filepath'] = './Public/Upload/'.$result['savepath'].$result['savename'];
        }

    	$Dao = M('Order');
    	$r = $Dao->add($data);

    	if($r){
    		$result = array('code' => 200, 'msg' => '订单添加成功');
    	} else{
    		$result = array('code' => 202, 'msg' => '订单添加失败');
    	}

    	echo json_encode($result);
    }
}